package com.wz.wiki.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wz.wiki.entity.pojo.Category;
import com.wz.wiki.entity.pojo.CategoryExample;
import com.wz.wiki.entity.req.CategoryQueryReq;
import com.wz.wiki.entity.req.CategorySaveReq;
import com.wz.wiki.entity.resp.CategoryQueryResp;
import com.wz.wiki.entity.resp.PageResp;
import com.wz.wiki.mapper.CategoryMapper;
import com.wz.wiki.service.CategoryService;
import com.wz.wiki.utils.CopyBeanUtils;
import com.wz.wiki.utils.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王哲
 * @Contact 1121586359@qq.com
 * @ClassName TestService.java
 * @create 2023年06月28日 下午2:53
 * @Description 电子书业务层接口实现类
 * @Version V1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    @Override
    public PageResp<CategoryQueryResp> selectList(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        // 创建查询条件
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        categoryExample.setOrderByClause("sort asc");
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        PageHelper.startPage(req.getPage(), req.getSize());
        List<Category> categorys = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> categoryPageInfo = new PageInfo<>(categorys);
        System.out.println("总行数：" + categoryPageInfo.getTotal());

        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(categoryPageInfo.getTotal());
        pageResp.setList(CopyBeanUtils.copyList(categorys, CategoryQueryResp.class));

        return pageResp;
    }

    @Override
    public List<CategoryQueryResp> selectAll(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();

        // 创建查询条件
        CategoryExample.Criteria criteria = categoryExample.createCriteria();

        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        categoryExample.setOrderByClause("sort asc");

        List<Category> categorys = categoryMapper.selectByExample(categoryExample);

        return CopyBeanUtils.copyList(categorys, CategoryQueryResp.class);
    }

    @Override
    public void save(CategorySaveReq req) {

        Long id = req.getId();
        Category category = CopyBeanUtils.copy(req, Category.class);
        if (id == null) {
            // 新增
            id = snowFlake.nextId();
            category.setId(id);
            categoryMapper.insert(category);
        }else {
            // 更新
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    @Override
    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
